﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;

namespace LiveFrameworkExplorer.Plugins.LiveFxBrowser
{
    internal static class LiveFxBrowserHelper
    {
        #region Internal

        internal static string FormatTextAsHtml(string text, bool encodeWhitespace)
        { return FormatTextAsHtml(text, encodeWhitespace, true); }
        internal static string FormatTextAsHtml(string text, bool encodeWhitespace, bool includeBodyTags)
        {
            string str = text.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
            if (encodeWhitespace)
            {
                str = str.Replace("\r\n", "<br>").Replace("\n", "<br>").Replace(" ", "&nbsp;");
            }
            else
            {
                str = "<pre id=preContent>" + str + "</pre>";
            }
            if (includeBodyTags)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("<html><head><style type=\"text/css\">");
                builder.Append("body, pre, table { font-size : 9pt }");
                builder.Append("body, pre, table { font-family : Consolas, Courier New, monospace }");
                builder.Append("td { vertical-align : top } ");
                builder.Append("</style></head><body>");
                builder.Append(str);
                builder.Append("</body></html>");
                str = builder.ToString();
            }
            return str;
        }

        internal static void TagWlcResources(XmlDocument xd, string tag, out List<string> markup)
        {
            markup = new List<string>();
            int num = 0;
            XmlNodeList list = xd.SelectNodes("descendant::*[@href]");
            for (int i = 0; i < list.Count; i++)
            {
                XmlNode n = list[i];
                XmlAttribute attribute = n.Attributes["href"];
                if ((attribute != null) && !string.IsNullOrEmpty(attribute.Value))
                {
                    string url = attribute.Value;
                    if (!url.StartsWith("http"))
                    {
                        url = GenerateAbsoluteUrl(url, n);
                    }
                    if (url != null)
                    {
                        markup.Add(string.Format("<a href=\"{0}\">", url));
                        markup.Add("</a>");
                        string str2 = string.Format(tag, num.ToString());
                        num++;
                        string str3 = string.Format(tag, num.ToString());
                        num++;
                        attribute.Value = str2 + attribute.Value + str3;
                    }
                }
            }
        }

        internal static void TagWlcResources(JsonItem item, string tag, ref List<string> markup, Uri baseUri)
        {
            if (item.Type == JsonItem.JsonItemType.Object)
            {
                foreach (JsonItem item2 in item.Children)
                {
                    if ((item2.Type == JsonItem.JsonItemType.Member) && (item2.Value == "\"BaseUri\""))
                    {
                        baseUri = new Uri(UnescapeJson(item2.Children[0].Value).Trim(new char[] { '"' }));
                    }
                }
            }
            else if (((item.Type == JsonItem.JsonItemType.Member) && (item.Value.EndsWith("Uri\"") || item.Value.EndsWith("Link\""))) && !string.IsNullOrEmpty(item.Children[0].Value))
            {
                string relativeUri = UnescapeJson(item.Children[0].Value).Trim(new char[] { '"' });
                if (!relativeUri.StartsWith("http"))
                {
                    relativeUri = new Uri(baseUri, relativeUri).AbsoluteUri;
                }
                string str2 = string.Format(tag, markup.Count.ToString());
                markup.Add(string.Format("<a href=\"{0}\">", relativeUri));
                string str3 = string.Format(tag, markup.Count.ToString());
                markup.Add("</a>");
                item.Children[0].Value = str2 + item.Children[0].Value + str3;
            }
            if (item.Children != null)
            {
                foreach (JsonItem item3 in item.Children)
                {
                    TagWlcResources(item3, tag, ref markup, baseUri);
                }
            }
        }

        internal static string ReplaceTagsWithMarkup(string html, string tag, List<string> markup)
        {
            for (int i = 0; i < markup.Count; i++)
            {
                string pattern = string.Format(tag, i);
                html = Regex.Replace(html, pattern, markup[i]);
            }
            return html;
        }

    #endregion

        #region Private

        private static string GenerateAbsoluteUrl(string url, XmlNode n)
        {
            while (n != null)
            {
                XmlAttribute attribute = n.Attributes["xml:base"];
                if ((attribute != null) && !string.IsNullOrEmpty(attribute.Value))
                {
                    Uri baseUri = new Uri(attribute.Value);
                    Uri uri2 = new Uri(baseUri, url);
                    return uri2.AbsoluteUri;
                }
                n = n.ParentNode;
            }
            return null;
        }

        private static string UnescapeJson(string s)
        {
            if (s == null)
            {
                return s;
            }
            return s.Replace(@"\/", "/");
        }

        #endregion
    }
}
